﻿var arrStack = new Array();
function DialogForm_ShowFrame(headerText, view, qs, event, w, h, dialogLoadingPanel) {
    if (!arrStack || arrStack.length == 0 || arrStack[arrStack.length - 1][2] != headerText) {
        w = w ? w : DialogForm_GetWidth(view);
        h = h ? h : DialogForm_GetHeight(view);
        var _window = this;
        var _src = 'PopUpPage.aspx?view=' + view;
        if (!isNullOrEmpty(qs))
            _src += '&' + qs;
        _src += '&wdth=' + w + '&hght=' + h + '&hdrTitle=' + headerText;
        while (_window.parent != _window) {
            _window = _window.parent;
        }
        var arr = new Array(3);
        arr[0] = event;
        _window.arrStack[_window.arrStack.length] = arr;
        var frameId = "modalDialog_" + _window.arrStack.length;
        var _frame = $('<iframe id="' + frameId
            + '" style="visibility:hidden; overflow: hidden; position:fixed; left:0; top:0; margin:0; padding:0; ' +
            'z-index:99999; border:none; background-color: transparent;" width="100%" height="100%" scrolling="no" ' +
            'frameborder="0" allowtransparency="true" src="' + _src + '" />');
        _frame.load(function () {
            if (dialogLoadingPanel)
                dialogLoadingPanel.Hide();
            $('#' + frameId).css('visibility', 'visible');
        });
        _frame.appendTo('body');
        arr[1] = frameId;
        arr[2] = headerText;
    } else {
        if (dialogLoadingPanel)
            dialogLoadingPanel.Hide();
    }
}
function RemoveLastWindow() {
    $('#' + arrStack[arrStack.length - 1][1]).remove();
    arrStack.pop();
}

function HideDialogFrame() {
    setTimeout(TimeOutHideDialogFrame, 100);
}

function TimeOutHideDialogFrame() {
    $('#' + arrStack[arrStack.length - 1][1]).remove();
    arrStack.pop();
}

var CloseDialogFrameValue = null;

function CloseDialogFrame(_val) {
    CloseDialogFrameValue = _val;
    if (__aspxBrowserMajorVersion == 9) {
        setTimeout(TimeOutCloseDialogFrame, 100);
    } else {
        TimeOutCloseDialogFrame();
    }
}

function TimeOutCloseDialogFrame() {
    var _val = CloseDialogFrameValue;
    CloseDialogFrameValue = null;
    var _fun;
    if (arrStack.length > 1) {
        $('#' + arrStack[arrStack.length - 1][1]).remove();
        _fun = arrStack[arrStack.length - 1][0];
        arrStack.pop();
        if (window.execScript) {
            execScript('document.getElementById(arrStack[arrStack.length - 1][1]).contentWindow.' + _fun + '(\'' + _val + '\');');
        } else {
            eval('document.getElementById(arrStack[arrStack.length - 1][1]).contentWindow.' + _fun + '(\'' + _val + '\');');
        }

    } else {
        $('#' + arrStack[arrStack.length - 1][1]).remove();
        _fun = arrStack[arrStack.length - 1][0];
        arrStack.pop();
        if (window.execScript) {
            execScript(getCompleteFunction(_fun, _val));
        } else {
            setTimeout(function () {
                eval(getCompleteFunction(_fun, _val));
            }, 100);
        }
    }
}

function getCompleteFunction(fun, val) {
    if (fun.charAt(fun.length - 1) == ";")
        return fun;
    else {
        return fun + '(\'' + val + '\');';
    }
}